<?php
class IndexAction extends Action
{
    public function index()
    {
        if (!$this->check())
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
        else if (session('power') == 1)
            redirect(__ROOT__ . '/Admin', 0, ' 登录成功！自动转跳中~	');
        
        $title = "未出厂飞机信息";
        
        $fly = M('fly_info');
        import('ORG.Util.Page'); // 导入分页类
        
        if (isset($_GET['context'])) {
            $sql = "no='" . $_GET['context'] . "' OR man='" . $_GET['context'] . "' AND state=1";
        } else {
            $sql = "state=1";
        }
        
        $count = $fly->where($sql)->count();
        $Page  = new Page($count, 10);
        $show  = $Page->show($sql);
        if ($count == 0 && $sql=="state=1") {
            $tips[0] = "数据库没有记录！";
			$tips[1] = "";
        }else if($count == 0){
			$tips[0] = "没有相关记录！";
			$tips[1] = "";
		}else{
			$tips[0] = "";
			$tips[1] = "";
		}
        
        $data = $fly->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $unit = M('unit');
        for ($i = 0; $i < count($data); $i++) {
            $name             = $unit->where('id=%d', $data[$i]['unit'])->find();
            $data[$i]['unit'] = $name['name'];
        }
        
        $this->assign("fly", $data);
        $this->assign('page', $show); //赋值分页输出
        $this->assign("title", $title);
        $this->assign("tips", $tips);
        $this->assign("search", "");
        $this->display();
    }
    
    public function login()
    {
        if ($this->check()) {
            if (session('power') == 1)
                redirect(__ROOT__ . '/Admin', 0, ' 登录成功！自动转跳中~	');
            else
                redirect(__ROOT__ . '/Index', 0, ' 登录成功！自动转跳中~	');
        }
        if (isset($_POST['username']) && isset($_POST['password'])) {
            $User = M("user");
            $data = $User->where("user = '%s'", $_POST['username'])->find();
            if ($data != false and $data != null) {
                if (md5(md5($_POST['password'])) == $data['pass']) {
                    $num = count($_POST["rememberme"]);
                    session('username', $_POST['username']);
                    session('power', $data['power']);
                    $id    = $data['id'];
                    $token = md5(_SAND_ . time() . $id);
                    
                    $login         = M('user_login');
                    $data['id']    = $id;
                    $data['token'] = $token;
                    if ($num != 0) {
                        $data['dietime'] = date("Y/m/d H:i:s", time() + 604800); //一个星期免登陆
                        cookie('token', $token, 604800);
                    } else {
                        $data['dietime'] = date("Y/m/d H:i:s", time() + 3600);
                        cookie('token', $token, 3600);
                    }
                    
                    $flag = $login->where("id = %d", $id)->find();
                    if ($flag) {
                        $login->where("id = %d", $id)->save($data);
                    } else {
                        $login->add($data);
                    }
                    if (session('power') == 1)
                        redirect(__ROOT__ . '/Admin', 0, ' 登录成功！自动转跳中~	');
                    else
                        redirect(__ROOT__ . '/Index', 0, ' 登录成功！自动转跳中~	');
                }
            }
            echo '<script>alert("用户名或密码错误请重新登陆");</script>';
            echo "<script>window.history.back(-1);</script>";
        }
        $this->display();
    }
    
    public function view()
    {
        $title = "照片浏览";
        $this->assign("search", "");
        
        $regexno  = '/^\d{4}$/';
        $regexman = '/^\d{7}$/';
        $matches  = array();
        $tips     = "";
        
        if ($this->check()) {
            $db = M("photos_info");
            import('ORG.Util.Page'); // 导入分页类
            
            //分页
            if (isset($_GET['context'])) {
                if (is_numeric($_GET['context'])) {
                    if (preg_match($regexman, $_GET['context'], $matches)) {
                        $db2 = M("fly_info");
                        $res = $db2->where("man = '%s'", $_GET['context'])->select();
                        
                        
                        if ($res) {
                            $count = 0;
                            for ($i = 0; $i < count($res); $i++) {
                                $count = $count + $db->where("plane='%s'", $res[$i]['no'])->count();
                            }
                            
                            //$count = $db->where()->count();
                            $Page = new Page($count, 12);
                            $show = $Page->show();
                            for ($i = 0; $i < count($res); $i++) {
                                $data = $db->where("plane='%s'", $res[$i]['no'])->select();
                                $data = array_slice($data, $Page->firstRow, $Page->listRows);
                            }
                        } else {
                            $count = $db->where()->count();
                            $Page  = new Page($count, 12);
                            $show  = $Page->show();
                            $data  = $db->where()->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        }
                        if ($data == null || $data == false)
                            $tips = "没有该记录!";
                    } else if (preg_match($regexno, $_GET['context'], $matches)) {
                        $count = $db->where("plane='%s'", $_GET['context'])->count();
                        $Page  = new Page($count, 12);
                        $show  = $Page->show();
                        $data  = $db->where("plane='%s'", $_GET['context'])->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        if ($data == null || $data == false)
                            $tips = "没有该记录!";
                    } else {
                        $count = $db->count();
                        $Page  = new Page($count, 12);
                        $show  = $Page->show();
                        $data  = $db->limit($Page->firstRow . ',' . $Page->listRows)->select();
                        if ($data == null || $data == false)
                            $tips = "输入格式不正确（将显示所有照片！）";
                    }
                } else {
                    $count = $db->count();
                    $Page  = new Page($count, 12);
                    $show  = $Page->show();
                    $data  = $db->limit($Page->firstRow . ',' . $Page->listRows)->select();
                    if ($data == null || $data == false)
                        $tips = "输入格式不正确（将显示所有照片！）";
                }
            } else {
                $count = $db->count();
                $Page  = new Page($count, 12);
                $show  = $Page->show();
                $data  = $db->limit($Page->firstRow . ',' . $Page->listRows)->select();
            }
            
            
            for ($i = 0; $i < count($data); $i++) {
                $name             = $data[$i]['url'];
                $temp             = explode("/", $name);
                $data[$i]['name'] = $temp[count($temp) - 1];
                
            }
            $this->assign("data", $data);
            $this->assign("title", $title);
            $this->assign('page', $show); // 赋值分页输出
            $this->assign('tips', $tips);
            $this->display();
        } else {
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
        }
        
    }
    
	
	public function info()
    {
        $title = "照片信息";
        
        $search = "disabled";
        
        $styles = array(
            "button" => array(
                "active",
                ""
            ),
            "box" => array(
                "",
                "display:none"
            ),
            "tips" => array(
                "display:none",
                "display:none"
            )
        );
        $Tdata  = null;
        $tips   = null;
        
        if($this->check()){
            $search   = "";
            $matches  = array();
            $regexman = '/^\d{7}$/';
            $regexno  = '/^\d{4}$/';
            if (isset($_GET['context'])) {
                if (preg_match($regexman, $_GET['context'], $matches)) {
                    $fly = M("fly_info");
                    $res = $fly->where("man='%s' and state=1", $_GET['context'])->find();
                    if ($res) {
                        $res = M("photos_info")->where("plane='%s'", $res['no'])->select();
                        if ($res) {
                            $Tdata  = $res;
                            $styles = array(
                                "button" => array(
                                    "",
                                    "active"
                                ),
                                "box" => array(
                                    "display:none",
                                    ""
                                ),
                                "tips" => array(
                                    "display:none",
                                    "display:none"
                                )
                            );
                        } else {
                            $styles = array(
                                "button" => array(
                                    "",
                                    "active"
                                ),
                                "box" => array(
                                    "display:none",
                                    ""
                                ),
                                "tips" => array(
                                    "display:none",
                                    ""
                                )
                            );
                            $tips   = "没有相关记录！";
                        }
                    } else {
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                ""
                            )
                        );
                        $tips   = "没有相关记录！";
                    }
                } else if (preg_match($regexno, $_GET['context'], $matches)) {
                    $res = M("photos_info")->where("plane='%s'", $_GET['context'])->select();
                    if ($res) {
                        $Tdata  = $res;
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                "display:none"
                            )
                        );
                    } else {
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                ""
                            )
                        );
                        $tips   = "没有相关记录！";
                    }
                } else if (is_numeric($_GET['context'])) {
                    $res = M("photos_info")->where("id=%d", $_GET['context'])->select();
                    if ($res) {
                        $Tdata  = $res;
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                "display:none"
                            )
                        );
                    } else {
                        $styles = array(
                            "button" => array(
                                "",
                                "active"
                            ),
                            "box" => array(
                                "display:none",
                                ""
                            ),
                            "tips" => array(
                                "display:none",
                                ""
                            )
                        );
                        $tips   = "没有相关记录！";
                    }
                } else {
                    $styles = array(
                        "button" => array(
                            "",
                            "active"
                        ),
                        "box" => array(
                            "display:none",
                            ""
                        ),
                        "tips" => array(
                            "display:none",
                            ""
                        )
                    );
                    $tips   = "没有相关记录！";
                }
                
            } else {
                $styles = array(
                    "button" => array(
                        "",
                        "active"
                    ),
                    "box" => array(
                        "display:none",
                        "display:none"
                    ),
                    "tips" => array(
                        "",
                        "display:none"
                    )
                );
            }
        
        
            
            
            $fly       = M("fly_info");
            $datas_fly = $fly->where("state=1")->select();
            $this->assign("fly", $datas_fly);
            
            $photos       = M("photos_position");
            $datas_photos = $photos->select();
            $this->assign("photos", $datas_photos);
            
            $majors       = M("major");
            $datas_majors = $majors->select();
            $this->assign("majors", $datas_majors);
            
            $wtimes       = M("wtime");
            $datas_wtimes = $wtimes->select();
            $this->assign("wtime", $datas_wtimes);
            
            $this->assign("styles", $styles);
            $this->assign("tips", $tips);
            $this->assign("Tdata", $Tdata);
            
            $this->assign('title', $title);
            $this->assign("search", $search);
            $this->display();
        } else
            redirect(__ROOT__ . '/Index/login', 0, ' <script>alert("请先登陆！")</script>');
    }
	
    public function logout()
    {
        cookie('token', null);
        redirect(__ROOT__ . '/Index', 0, '');
    }
    
    //判断登陆是否过时
    private function check()
    {
        $cookie = cookie('token');
        $login  = M('user_login');
        $data   = $login->where('token="%s"', $cookie)->find();
        
        if ($cookie != null && $cookie != "" && $data != null && $data != false) {
            $id   = $data['id'];
            $date = $data['dietime'];
            
            if (strtotime($date) > time()) {
                $User = M("user");
                $user = $User->where("id = '%d'", $id)->find();
                session('username', $user['user']);
                session('power', $user['power']);
                return true;
            }
        }
        return false;
    }
}